import 'dart:convert' show json;
import 'package:flutter/foundation.dart';
import 'package:loxen/mode/master/file_list_bean.dart';
import 'package:rxdart/rxdart.dart';
import 'package:loxen/api/api.dart';

mixin FileChangeDeleteAddSelectMixin {
  //文件名称修改 返回200为修改成功
  Stream<int> fileChangeName(String id, {String name, bool isHide}) {
    if (name != null && isHide != null) {
      return Rx.zip2<int, int, int>(_reName(id, name), _setHide(id, isHide),
          (a, b) {
        if (a == 200 && b == 200) {
          return 200;
        } else {
          return 400;
        }
      });
    } else {
      if (name != null) {
        return _reName(id, name);
      } else {
        return _setHide(id, isHide);
      }
    }
  }

  Stream<int> setHideAll(Map data) {
    return ApiService.fileHide(data)
        .flatMap((value) => Stream.value(value.statusCode));
  }

  Stream<int> _reName(String id, String name) {
    return ApiService.reNameFile(id, name)
        .flatMap((value) => Stream.value(value.statusCode));
  }

  Stream<int> _setHide(String id, bool isHide) {
    Map data = {"$id": isHide};
    return ApiService.fileHide(data)
        .flatMap((value) => Stream.value(value.statusCode));
  }

  //文件删除 返回200为删除成功
  Stream<int> fileDelete(String ids) {
    return ApiService.deleteFiles(ids)
        .flatMap((value) => Stream.value(value.statusCode));
  }

  //添加新文件
  Stream<int> fileAdd(String name, String type, bool isHide) {
    return ApiService.createFile(name, type, isHide)
        .flatMap((value) => compute(deCodeData, value.data).asStream())
        .flatMap((value) => Stream.value(value?.data?.id));
  }

  //查询文件列表
  Stream<FileListBean> fileSelect(String type,
      {String page = '1', String userId, String pageSize}) {
    return ApiService.fileList(type, page, userId, pageSize)
        .flatMap((value) => compute(decodeFileListBean, value.data).asStream());
  }

  static FileListBean decodeFileListBean(dynamic json) {
    return FileListBean.fromJson(json);
  }

  static Root deCodeData(dynamic json) {
    return Root.fromJson(json);
  }
}

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class Root {
  Root({
    this.code,
    this.data,
  });

  factory Root.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Root(
          code: asT<int>(jsonRes['code']),
          data: Data.fromJson(asT<Map<String, dynamic>>(jsonRes['data'])),
        );

  int code;
  Data data;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'code': code,
        'data': data,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Data {
  Data({
    this.id,
  });

  factory Data.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Data(
          id: asT<int>(jsonRes['id']),
        );

  int id;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
